
package com.huaweicloud.haydn.delivercore.agent;

import com.huaweicloud.haydn.delivercore.agent.service.ScheduledTask;
import com.huaweicloud.haydn.delivercore.agent.utils.EncryptUtil;

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * @since 2022-09-24
 */
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@ComponentScan(value = "com.huaweicloud")
@EnableAsync
@EnableScheduling
public class AgentApplication {
    public static void main(String[] args) {
        if (args.length > 0 && "encrypt".equals(args[0])) {
            String key = args[1];
            String password = args[2];
            EncryptUtil encryptUtil = new EncryptUtil();
            PooledPBEStringEncryptor encryptor = encryptUtil.getEncryptor(key);
            String result = encryptor.encrypt(password);
            System.out.println("ENC(" + result + ")");
            System.exit(1);
        }
        ConfigurableApplicationContext applicationContext = SpringApplication.run(AgentApplication.class, args);
        if (args.length > 0 && "once".equals(args[0])) {
            ScheduledTask scheduledTask = applicationContext.getBean(ScheduledTask.class);
            scheduledTask.onceRun();
            applicationContext.close();
            System.exit(1);
        }
    }
}
